package com.uduew.sjd.ui.mine

import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.api.AddBankInfoApi
import com.uduew.sjd.api.AppointFormApi
import com.uduew.sjd.bean.HttpBean
import com.uduew.sjd.config.Const
import com.uduew.sjd.message.Messages
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.AddBankInfoRequester
import com.uduew.sjd.request.AppointFormRequester
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.MainHandler
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.utils.Utils
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.raedev.forms.FormGroupAdapter
import com.raedev.forms.dict.FormSelectItem
import com.raedev.forms.dict.SimpleDataProvider
import com.raedev.forms.render.DataBindingFormRender

class AddBankFragment : BaseFragment() {

    lateinit var mState: AddBankStates
    lateinit var render: DataBindingFormRender
    private val mAdapter = FormGroupAdapter()
    private lateinit var mAddBankInfoRequester: AddBankInfoRequester
    private lateinit var mAppointFormRequester: AppointFormRequester
    private lateinit var mMessenger: PageMessenger

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(AddBankStates::class.java)
        mAddBankInfoRequester = getFragmentScopeViewModel(AddBankInfoRequester::class.java)
        mAppointFormRequester = getFragmentScopeViewModel(AppointFormRequester::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        this.render = DataBindingFormRender(
            requireContext(),
            mAdapter,
            requireActivity().supportFragmentManager
        )
        render.render()
        mAddBankInfoRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<Any>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            TipDialog.show("添加成功")
            MainHandler.postDelay(1000) {
                nav().navigateUp()
            }
            mMessenger.input(Messages(Messages.EVENT_ADD_BANK_SUCCESS))
        }
        //获取指定表单
        mAppointFormRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<AppointFormApi.AbsquatulationBean>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.data.let {
                if (it != null && it.mothery != null && it.mothery.isNotEmpty()) {
                    val mothery = it.mothery[0]
                    mothery.thorough.forEach { item ->
                        if (item.tabasco == Const.OLGA) {
                            val data = mutableListOf<FormSelectItem>().apply {
                                if (!item.mopishly.isNullOrEmpty()) {
                                    item.mopishly.forEach { item ->
                                        val selectItem =
                                            FormSelectItem(item.giglot, item.beastly, order = -1)
                                        this.add(selectItem)
                                        if (item.swipes != null && item.subDefineList != null) {
                                            item.subDefineList.forEachIndexed { index, subItem ->
                                                if (selectItem.value != subItem.beastly) {
                                                    selectItem.addChild(
                                                        FormSelectItem(
                                                            subItem.giglot,
                                                            item.beastly + Const.CONNECTOR + subItem.beastly,
                                                            order = -1
                                                        )
                                                    )
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            render.addSelect(
                                SimpleDataProvider(data),
                                item.giglot,
                                item.beastly,
                                null,
                                dialogTitle = getString(R.string.choose) + " " + item.giglot,
                                required = true
                            )
                        } else if (item.tabasco == Const.SYMPHYSIS) {
                            render.addEditText(item.giglot, item.beastly, "", required = true)
                        } else if (item.tabasco == Const.FAUCIAL) {
                            render.addNumberEditText(item.giglot, item.beastly, "", required = true)
                        }
                    }
                }
            }
        }

        WaitDialog.show(getString(R.string.loading))

        mAppointFormRequester.request(viewLifecycleOwner, "F561466", Const.THEOLOGIST)
    }

    private fun onItemClick(item: FormSelectItem) {

    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_add_bank, BR.vm, mState)
            .addBindingParam(BR.click, ClickProxy()).addBindingParam(BR.formAdapter, mAdapter)
            .addBindingParam(BR.titleBarListener, TitleBarListener())

    }

    inner class TitleBarListener : OnTitleBarListener {
        override fun onLeftClick(titleBar: TitleBar?) {
            super.onLeftClick(titleBar)
            if (!Utils.checkDebounce()) return
            nav().navigateUp()
        }

        override fun onRightClick(titleBar: TitleBar?) {
            super.onRightClick(titleBar)
        }
    }

    class AddBankStates : StateHolder() {
    }

    open inner class ClickProxy {
        fun addBank() {
            if (!Utils.checkDebounce()) return
            var formGroup = mAdapter.formGroup
            var map = formGroup.toMap()
            val result = formGroup.validateForm()
            if (!result.successfully()) {
                TipDialog.show(result.message)
                return
            }
            WaitDialog.show(getString(R.string.loading))
            val bean = AddBankInfoApi.ReqAddBankInfoBean()

            bean.setCum(map["ofs.cum"])
            bean.setMelaleuca(map["ofs.melaleuca"])
            bean.setAerification(map["ofs.aerification"])
            mAddBankInfoRequester.request(this@AddBankFragment, bean)
        }
    }

}